package com.jwb.pub.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.service.MenuService;
import com.jwb.util.MainController;
import com.jwb.util.ToolsUtil;


/**
 * 菜单
 * @author liuyn
 *
 */
@Slf4j
@Controller
@RequestMapping(value="/pub/menu")
public class MenuController extends MainController{


	
	@Autowired
	private MenuService menuService;
	
    
	/**
	 * http://127.0.0.1:8080/ssm/pub/menu/getSysMenu?sysName=
	 * @description：显示权限列表
	 * @author liuyn
	 * @return	
	 * @createDate 2018-3-15
	 * @createTime 下午5:04:33
	 */
    @RequestMapping(value="/getSysMenu")
    @ResponseBody
	public String getSysMenu(HttpServletRequest req) throws Exception
	{
    	JSONObject jsobject = new JSONObject();	
    	JSONObject jsdata = new JSONObject();
    	String sysName = req.getParameter("sysName") == null ? "" : req.getParameter("sysName");
    	jsdata.put("sysName", sysName);
    	log.info("getSysMenu- sysName "+sysName);
    	HttpSession session = req.getSession();    	
    	String privNames= (String)session.getAttribute("privNames") == null ? "" : (String)session.getAttribute("privNames");
       	jsdata.put("privNames", privNames);
    	log.info("getSysMenu privNames "+privNames);
    	jsobject = menuService.getSysMenu(jsdata);		
		log.info("getSysMenu "+jsobject.toString());
		return jsobject.toString();
	}
    
    
    
    /**
	 * http://127.0.0.1:8080/ssm/pub/menu/getNewMenu?sysName=&privNames=
	 * @description：显示权限列表
	 * @author liuyn
	 * @return	
	 * @createDate 2018-3-15
	 * @createTime 下午5:04:33
	 */
    @RequestMapping(value="/getNewMenu")
    @ResponseBody
	public String getNewMenu(HttpServletRequest req) throws Exception{
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = new JSONObject();
    	String sysName = req.getParameter("sysName") == null ? "" : req.getParameter("sysName");
    	log.info("getSysMenu- sysName "+sysName);
    	jsdata.put("sysName",sysName);
    	HttpSession session = req.getSession();
    	String privNames=(String)session.getAttribute("privNames") == null ? "" : (String)session.getAttribute("privNames");
    	log.info("getSysMenu privNames "+privNames);
    	jsdata.put("privNames", privNames);
    	jsobject = menuService.getNewMenu(jsdata);    	
    	log.info("getNewMenu "+jsobject.toString());
    	return jsobject.toString();
    }
    
    

    /**
     * http://127.0.0.1:8080/ssm/pub/menu/menuTree?sysName=
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/menuTree")
    @ResponseBody
	public String menuTree(HttpServletRequest req) throws Exception
	{
    	JSONObject jsobject = new JSONObject();	
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("sysName", req.getParameter("sysName") == null ? "" : req.getParameter("sysName"));
		jsdata.put("superior", req.getParameter("id")==null?"-1":req.getParameter("id"));
    	jsobject= menuService.getMenuTreeJson(jsdata);		
		log.info("menuTree "+jsobject.toString());
		return jsobject.toString();
	}
    
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/menuInfo?menuId=
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/menuInfo")
    @ResponseBody
    public String menuInfo(HttpServletRequest req){
    	JSONObject jsobject= new JSONObject();
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("menuId", req.getParameter("menuId")==null?"":req.getParameter("menuId"));
    	jsobject = menuService.getMenuInfo(jsdata);
    	log.info("menuInfo "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/btnInfo?menuId=&menuLevel=
     * @param req
     * @return
     */
    @RequestMapping(value="/btnInfo")
    @ResponseBody
    public String btnInfo(HttpServletRequest req){
    	JSONObject jsdata =new JSONObject();
    	jsdata.put("menuId", req.getParameter("menuId")== null?"":req.getParameter("menuId"));
    	jsdata.put("menuLevel", req.getParameter("menuLevel") ==null?"":req.getParameter("menuLevel"));
    	JSONObject jsobject = new JSONObject();
    	jsobject = menuService.btnInfo(jsdata);    	
    	log.info("btnInfo "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/delMenu?menuId=
     * @param req
     * @return
     */
    @RequestMapping(value="/delMenu")
    @ResponseBody
    public String delMenu(HttpServletRequest req){
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("menuId", req.getParameter("menuId")== null?"":req.getParameter("menuId"));
    	jsobject = menuService.delMenu(jsdata);
    	log.info("delMenu "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/menuRights
     * @param req
     * @return
     */
    @RequestMapping(value="/menuRights")
    @ResponseBody
    public String menuRights(HttpServletRequest req){
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = new JSONObject();
    	jsobject = menuService.getMenuRights(jsdata);   
    	log.info("menuRights "+jsobject.toString());
    	return jsobject.toString();
    }
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/picPhoto?file
     * @param req
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/picPhoto")
    @ResponseBody
    public String picPhoto(HttpServletRequest req, @RequestParam(value="file")MultipartFile file) throws Exception
	  {
    	JSONObject jsobject = new JSONObject();	
    	
		try
		{
			String fileName=ToolsUtil.decode(req.getParameter("fileName"));
			String rootPath = req.getServletContext().getRealPath("/");
			// 有文件
			if (file != null)
			{
				String saveName = "";
				// 获取该文件扩展名并统一转换为小写
				String fileExt="";
				fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
				String fileId=ToolsUtil.get32bituuid();				
				saveName = fileId + fileExt;
				//String attachPath ="/scafter/activity/images/upload";
				String attachPath ="/file/images/upload/menu";
				String filePath =  rootPath + attachPath+"/" + saveName; 
				File f = new File(rootPath+attachPath);
	    		f.mkdirs();
				
				InputStream fis = file.getInputStream();
				FileOutputStream fos = new FileOutputStream(filePath);
				
				try
				{
					byte[] b = new byte[1024];
					int len = 0;
					while ((len = fis.read(b)) > 0)
					{
						fos.write(b, 0, len);
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					fis.close();
					fos.close();
				}							
				
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
				jsobject.put("fileName", attachPath+"/"+saveName);
			}
			else
			{
				jsobject.put("respCode", "999");
				jsobject.put("respDesc", "修改LOGO失败");
			}
			
		}
		catch (Exception e)
		{
			jsobject.put("respCode", "1000");
			jsobject.put("respDesc", "修改LOGO失败");
		}
		log.info("picPhoto"+jsobject.toString());
		return jsobject.toString();  
	}
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/saveMenu?operType=&menuId=&menuLevel=&sysName=&jsonStr=
     */
    @RequestMapping(value="/saveMenu")
    @ResponseBody
    public String saveMenu(HttpServletRequest req){
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("operType", req.getParameter("operType") ==null?"":req.getParameter("operType"));
    	jsdata.put("menuId", req.getParameter("menuId") ==null?"":req.getParameter("menuId"));
    	jsdata.put("menuLevel", req.getParameter("menuLevel") ==null?"":req.getParameter("menuLevel"));
    	jsdata.put("sysName", req.getParameter("sysName")==null?"":req.getParameter("sysName"));
    	jsdata.put("jsonStr", req.getParameter("jsonStr") == null?"{}":req.getParameter("jsonStr"));
    	jsobject = menuService.saveMenu(jsdata);
    	log.info("saveMenu "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     *  http://127.0.0.1:8080/ssm/pub/menu/getAppSysMenu?sysName=sys_name_1
     * @param req
     * @return
     * @throws Exception
     */

    @RequestMapping(value="/getAppSysMenu")
    @ResponseBody
	public String getAppSysMenu(HttpServletRequest req) throws Exception
	{
    	JSONObject jsdata = new JSONObject();
    	String sysName = req.getParameter("sysName") == null ? "" : req.getParameter("sysName");
    	log.info("getAppSysMenu- sysName "+sysName);
    	jsdata.put("sysName", sysName);
    	HttpSession session = req.getSession();
    	JSONObject jsobject = new JSONObject();	
    	String privNames=(String)session.getAttribute("privNames") == null ? "" : (String)session.getAttribute("privNames");
       	log.info("getAppSysMenu privNames "+privNames);
       	jsdata.put("privNames", privNames);
    	jsobject=menuService.getmenulist(jsdata);
    	jsobject=menuService.getAppSysMenu(jsobject, jsdata);		
		log.info("getAppSysMenu "+jsobject.toString());
		return jsobject.toString();
	}
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/sysMenus?currentPageNbr=1&pageSize=10
     * @param req
     * @return
     */
    @RequestMapping(value="/sysMenus")
    @ResponseBody
    public String sysMenus(HttpServletRequest req){
    	String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");
    	JSONObject jsobject = new JSONObject();
    	this.pageInit();
    	this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
    	this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
    	JSONObject jsdata = new JSONObject();
    	jsobject = menuService.sysMenus(this.getPage(),jsdata);    	
    	log.info("getSysMenu: "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/addSysMenu?code=&desc=
     * @param req
     * @return
     */
    @RequestMapping(value="/addSysMenu")
    @ResponseBody
    public String addSysMenu(HttpServletRequest req){
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("code", req.getParameter("code") == null ? "" : req.getParameter("code"));
    	jsdata.put("desc", req.getParameter("desc") == null ? "" : req.getParameter("desc"));
    	JSONObject jsobject = new JSONObject();
    	jsobject = menuService.addSysMenu(jsdata);    	
    	log.info("addSysMenu: "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/delSysMenu?menuId=
     * @param req
     * @return
     */
    @RequestMapping(value="/delSysMenu")
    @ResponseBody
    public String delSysMenu(HttpServletRequest req){
    	JSONObject jsdata = new JSONObject();
    	jsdata.put("menuId", req.getParameter("menuId") ==null?"":req.getParameter("menuId"));
    	JSONObject jsobject = new JSONObject();
    	jsobject = menuService.delMenu(jsdata);
    	log.info("delSysMenu: "+jsobject.toString());
    	return jsobject.toString();
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/menu/sysMenu?
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/sysMenu")
    @ResponseBody
	public String sysMenu(HttpServletRequest req) throws Exception
	{
    	String sysName = req.getParameter("sysName") == null ? "" : req.getParameter("sysName");
    	log.info("sysMenu- sysName "+sysName);
    	HttpSession session = req.getSession();
    	JSONObject jsobject = new JSONObject();	
    	String privNames=(String)session.getAttribute("privNames") == null ? "" : (String)session.getAttribute("privNames");
       	
    	log.info("getSysMenu privNames "+privNames);
    	jsobject = menuService.sysMenu(sysName, privNames);
		
		log.info("getSysMenu "+jsobject.toString());
		return jsobject.toString();
	}
    
}
